#ifndef RENDERTHREAD_H
#define RENDERTHREAD_H

#define MAX(a, b) ((a) < (b) ? (b) : (a))

#include "complex.h"
#include <QMutex>
#include <QSize>
#include <QThread>
#include <QTransform>
#include <QWaitCondition>
#include <QRgb>

class QImage;

class RenderThread : public QThread
{
    Q_OBJECT

public:
    RenderThread(QObject *parent = 0);
    ~RenderThread();

    void render(int numIterations, const Complex &lambda,
                const QTransform &inverseMatrix, const QSize &resultSize);

signals:
    void renderedImage(const QImage &image);

protected:
    void run();
    virtual int pointIteration(Complex z, int k, const Complex &c) { return 0; };
    virtual int pointIteration(double x, double y, int k, double cr, double ci) { return 0; };

private:
    QMutex mutex;
    QWaitCondition condition;
    bool restart;
    bool abort;

    QSize resultSize;
    QTransform inverseMatrix;
    Complex lambda;
    int numIterations;

    enum { ColormapSize = 16 };
    uint colormap[ColormapSize];
};

#endif
